import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "../stores/user";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/cake",
    },
    {
      path: "/cake",
      name: "cake-index",
      component: () => import("../views/cake/IndexView.vue"),
      meta: {
        requiresAuth: true,
        title: "表单",
      },
      children: [
        {
          path: "",
          name: "cake-list",
          component: () => import("../views/cake/CakeListView.vue"),
          meta: {
            requiresAuth: true,
            title: "列表",
          },
        },
        {
          path: ":id",
          name: "cake-detail",
          component: () => import("../views/cake/CakeDetailView.vue"),
          meta: {
            requiresAuth: true,
            title: "详情",
          },
        },
        {
          path: "add",
          name: "cake-add",
          component: () => import("../views/cake/CakeAddView.vue"),
          meta: {
            requiresAuth: true,
            title: "新增",
          },
        },
        {
          path: "edit/:id",
          name: "cake-edit",
          component: () => import("../views/cake/CakeEditView.vue"),
          meta: {
            requiresAuth: true,
            title: "编辑",
          },
        },
      ],
    },
    {
      path: "/cakeshelf",
      name: "cakeshelf-index",
      component: () => import("../views/cakeshelf/IndexView.vue"),
      meta: {
        requiresAuth: true,
        title: "我的橱窗",
      },
      children: [
        {
          path: "",
          name: "cakeshelf-list",
          component: () => import("../views/cakeshelf/cakeshelfListView.vue"),
          meta: {
            requiresAuth: true,
            title: "列表",
          },
        },
      ],
    },
    {
      path: "/user",
      name: "user-index",
      component: () => import("../views/user/IndexView.vue"),
      meta: {
        requiresAuth: true,
        title: "我的",
      },
      children: [
        {
          path: "",
          name: "user-profile",
          alias: "profile",
          component: () => import("../views/user/ProfileView.vue"),
          meta: {
            requiresAuth: true,
            title: "个人信息",
          },
        },
        {
          path: "change-profile/:id",
          name: "user-profile-esit",
          component: () => import("../views/user/ProfileEditView.vue"),
          meta: {
            requiresAuth: true,
            title: "修改个人信息",
          },
        },
        {
          path: "change-password/:id",
          name: "user-password-esit",
          component: () => import("../views/user/PasswordEditView.vue"),
          meta: {
            requiresAuth: true,
            title: "修改密码",
          },
        },
      ],
    },

    {
      path: "/login",
      name: "user-login",
      component: () => import("../views/user/LoginView.vue"),
      meta: {
        requiresAuth: false,
        title: "登录",
      },
    },
    {
      path: "/admin",
      name: "admin-index",
      component: () => import("../views/admin/IndexView.vue"),
      meta: {
        requiresAuth: true,
        requiresAdmin: true,
        title: "系统管理",
      },
      children: [
        {
          path: "",
          name: "user-list",
          alias: "user",
          component: () => import("../views/admin/UserListView.vue"),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
            title: "用户管理",
          },
        },
      ],
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
      meta: {
        requiresAuth: false,
        title: "关于",
      },
    },
    {
      path: "/:pathMatch(.*)*",
      name: "not-found",
      component: () => import("../views/NotFoundView.vue"),
      meta: {
        requiresAuth: false,
        title: "404",
      },
    },
  ],
});

//前置路由守卫
router.beforeEach((to) => {
  const store = useUserStore();
  const isLogin = store.isLogin;
  const isAdmin = store.isAdmin;
  console.log(`to:${to.fullPath},isLogin:${isLogin}`);

  //登录状态服务端校验
  if (to.meta.requiresAuth && isLogin) {
    console.log("后端校验权限");
    store.isAuth();
  }

  //防止登录页循环访问
  if (isLogin && to.name === "user-login") {
    return { path: "/" };
  }

  //未登录且需要访问权限，跳转到登录路由
  if (to.meta.requiresAuth && !isLogin) {
    return {
      path: "/login",
    };
  }

  //需要管理员权限，跳转到用户个人信息路由
  if (to.meta.requiresAdmin && !isAdmin) {
    return {
      path: "/user",
    };
  }
});

export default router;
